package com.lovetemperature.model;

import java.util.ArrayList;
import java.util.List;

public class Cart {
	private List<Stock> stockList = new ArrayList<Stock>(); // 购物车中商品对应的库存记录
	private List<GoodSet> goodSetList = new ArrayList<GoodSet>(); // 加到购物车中的商品

	public void addGood(Stock stock) {
		boolean found = false;
		int index;
		for (index = 0; index < stockList.size(); index++) {
			Stock oneStock = stockList.get(index);
			if (oneStock.getStockId().equals(stock.getStockId())) { // 已有重复的商品
				found = true;
				break;
			}
		}
		if (found) {
			Stock theStock = stockList.get(index);
			GoodSet theGoodSet = goodSetList.get(index);
			if (theStock.getGoodSet().getCount() - theGoodSet.getCount() > 0) { // 商品还有剩余
				theGoodSet.setCount(theGoodSet.getCount() + 1);
			} else {
				// 不能添加了
			}
		} else {
			stockList.add(stock);
			GoodSet newGoodSet = new GoodSet();
			newGoodSet.setCount(1);
			newGoodSet.setGood(stock.getGoodSet().getGood());
			goodSetList.add(newGoodSet);
		}
	}

	public List<Order> generateOrderList() {
		List<Order> result = new ArrayList<Order>();
		int index;
		for (index = 0; index < goodSetList.size(); index++) {
			GoodSet theGoodSet = goodSetList.get(index);
			
			Order order = new Order();
			order.setGoodSet(theGoodSet);
			result.add(order);
		}
		return result;
	}

	public List<Stock> generateStockList() {
		int index;
		for (index = 0; index < goodSetList.size(); index++) {
			GoodSet theGoodSet = goodSetList.get(index);
			Stock theStock = stockList.get(index);
			int remainCount = theStock.getGoodSet().getCount()
					- theGoodSet.getCount();
			theStock.getGoodSet().setCount(remainCount);
		}
		return stockList;
	}

	public List<Stock> getStockList() {
		return stockList;
	}

	public void setStockList(List<Stock> stockList) {
		this.stockList = stockList;
	}

	public List<GoodSet> getGoodSetList() {
		return goodSetList;
	}

	public void setGoodSetList(List<GoodSet> goodSetList) {
		this.goodSetList = goodSetList;
	}
}
